package com.quexin.pickmedialib

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.widget.SeekBar
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_preview_video.*
import org.jetbrains.anko.startActivity
import java.io.File

class PreviewVideoActivity : Activity() {

    companion object {

        private const val paramsPath = "paramsPath"

        @JvmStatic
        fun show(context: Context, path: String) {
            context.startActivity<PreviewVideoActivity>(paramsPath to path)
        }
    }

    private var isTrackingTouch = false
    private val handler = object : Handler(Looper.getMainLooper()) {
        val runnable = Runnable { start() }

        fun start() {
            sendEmptyMessage(0)
        }

        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if (video_view.isPlaying) {
                if (!isTrackingTouch) {
                    sb_video_progress.progress = video_view.currentPosition
                    tv_time_start.text = MediaUtils.updateTime2(sb_video_progress.progress.toLong())
                }
                postDelayed(runnable, 50)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_preview_video)
        init()
    }


    private fun init() {
        qib_back.setOnClickListener { finish() }

        val videoPath = intent.getStringExtra(paramsPath)
        if (videoPath.isNullOrEmpty() || !(File(videoPath).exists())) {
            Toast.makeText(this, "视频错误或不存在！", Toast.LENGTH_SHORT).show()
            finish()
            return
        }

        video_view.setVideoPath(videoPath)
        video_view.setOnPreparedListener {
            sb_video_progress.max = it.duration
            tv_time_end.text = MediaUtils.updateTime2(it.duration.toLong())
            video_view.start()
            handler.start()
        }
        video_view.setOnCompletionListener {
            video_view.seekTo(0)
            video_view.start()
        }
        sb_video_progress.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                tv_time_start.text = MediaUtils.updateTime2(progress.toLong())
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
                isTrackingTouch = true
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                isTrackingTouch = false
                video_view.seekTo(sb_video_progress.progress)
            }

        })
    }

    override fun onResume() {
        super.onResume()
        if (!video_view.isPlaying) {
            video_view.seekTo(sb_video_progress.progress)
        }
    }

    override fun onPause() {
        super.onPause()
        video_view.pause()
    }

}